#include<iostream>
using namespace std;
int x[100];
bool Place(int k){
    for(int i=1;i<k;i++)
    if(x[i]==x[k]||abs(i-k)==abs(x[i]-x[k])) return false;
    return true;    
}
int Quee(int n){
    int count=0;
    for(int i=0;i<=n;i++) x[i]=0;
    int k=1;
    while(k>=1){
        x[k]=x[k]+1;
        while(x[k]<=n&&(!Place(k))) x[k]=x[k]+1;
        if(k==n&&x[k]<=n){
            count++;
            for(int i=1;i<=k;i++) cout<<x[k]<<" ";
            cout<<endl;
        }
        else if(k<n&&x[k]<=n) k=k+1;
        else {
            x[k]=0;
            k=k-1;
        }
    }
    return count;
}
int main(){
    cout<<Quee(8)<<endl;
    return 0;
}